package com.spic.business.plan.team.service.impl;

import java.util.List;

import com.spic.business.plan.team.service.IPlanGroupService;
import com.spic.business.plan.team.service.IPlanPostService;
import com.spic.business.plan.team.service.IPlanTeamService;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spic.business.plan.team.mapper.PlanUserMapper;
import com.spic.business.plan.team.domain.PlanUser;
import com.spic.business.plan.team.service.IPlanUserService;
import com.spic.common.security.utils.cache.UserCacheUtils;

/**
 * 应急人员Service业务层处理
 *
 * @author spic
 * @date 2021-11-10
 */
@Service
public class PlanUserServiceImpl implements IPlanUserService
{
    @Autowired
    private PlanUserMapper planUserMapper;
    @Autowired
    private IPlanPostService planPostService;

    @Autowired
    private IPlanGroupService planGroupService;
    @Autowired
    private IPlanTeamService planTeamService;




    /**
     * 查询应急人员
     *
     * @param id 应急人员主键
     * @return 应急人员
     */
    @Override
    public PlanUser selectPlanUserById(Long id) {
        PlanUser planuser = planUserMapper.selectPlanUserById(id);
        try {
            planuser.setUserName(UserCacheUtils.getValue(planuser.getUserId()));
            planuser.setDeptId(Long.valueOf(UserCacheUtils.getUserDeptId(planuser.getUserId())));
            planuser.setPostName(planPostService.selectPlanPostById(planuser.getPostId()).getPostName());
            planuser.setGroupName(planGroupService.selectPlanGroupById(planuser.getGroupId()).getGroupName());
            planuser.setTeamName(planTeamService.selectPlanTeamById(planuser.getTeamId()).getTeamName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return planuser;
    }

    /**
     * 查询应急人员列表
     *
     * @param planUser 应急人员
     * @return 应急人员
     */
    @Override
    public List<PlanUser> selectPlanUserList(PlanUser planUser) throws Exception {
        List<PlanUser> planUsers=planUserMapper.selectPlanUserList(planUser);
        for (PlanUser planuser:planUsers) {
            planuser.setUserName(UserCacheUtils.getValue(planuser.getUserId()));
            planuser.setDeptId(Long.valueOf(UserCacheUtils.getUserDeptId(planuser.getUserId())));
            planuser.setPostName(planPostService.selectPlanPostById(planuser.getPostId()).getPostName());
            planuser.setGroupName(planGroupService.selectPlanGroupById(planuser.getGroupId()).getGroupName());
            planuser.setTeamName(planTeamService.selectPlanTeamById(planuser.getTeamId()).getTeamName());
        }
        return planUsers;
    }

    /**
     * 新增应急人员
     *
     * @param planUser 应急人员
     * @return 结果
     */
    @Override
    public int insertPlanUser(PlanUser planUser)
    {
        planUser.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        planUser.setCreateTime(DateUtils.getNowDate());
        return planUserMapper.insertPlanUser(planUser);
    }

    /**
     * 修改应急人员
     *
     * @param planUser 应急人员
     * @return 结果
     */
    @Override
    public int updatePlanUser(PlanUser planUser)
    {
        return planUserMapper.updatePlanUser(planUser);
    }

    /**
     * 批量删除应急人员
     *
     * @param ids 需要删除的应急人员主键
     * @return 结果
     */
    @Override
    public int deletePlanUserByIds(Long[] ids)
    {
        return planUserMapper.deletePlanUserByIds(ids);
    }

    /**
     * 删除应急人员信息
     *
     * @param id 应急人员主键
     * @return 结果
     */
    @Override
    public int deletePlanUserById(Long id)
    {
        return planUserMapper.deletePlanUserById(id);
    }
}
